package com.yufei.toolinterface.listener;

import it.sauronsoftware.cron4j.TaskExecutor;
import it.sauronsoftware.cron4j.TaskExecutorListener;

import javax.swing.JFrame;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.yufei.infoExtractor.exception.TaskResultExportException;
import com.yufei.infoExtractor.listener.AbstractSchedulerListener;
import com.yufei.infoExtractor.pfw.entity.InfoExtractTaskStatistics;
import com.yufei.infoExtractor.pfw.entity.News;
import com.yufei.infoExtractor.pfw.entity.Task;
import com.yufei.infoExtractor.task.InfoExtractorCommonTask;
import com.yufei.infoExtractor.util.ExceptionUtil;
import com.yufei.toolinterface.TaskMPanel;

public class SwingSchedulerListener extends AbstractSchedulerListener {
	private static Log mLog = LogFactory.getLog(SwingSchedulerListener.class);

private JFrame jframe=null;
	public SwingSchedulerListener(JFrame jframe) {
		super();
		this.jframe=jframe;
		// TODO Auto-generated constructor stub
	}
	@Override
	public void taskLaunching(TaskExecutor arg0) {
		// TODO Auto-generated method stub
		

	  TaskMPanel taskMPanel=(TaskMPanel) jframe;

	  it.sauronsoftware.cron4j.Task task= arg0.getTask();
      if(task.getClass().isAssignableFrom(InfoExtractorCommonTask.class)){
    	  InfoExtractorCommonTask infoExtractorCommonTask=(InfoExtractorCommonTask) task;
    	  String taskName=infoExtractorCommonTask.getTask().getTaskName();
          mLog.info("the task "+taskName+" starts running!");
          taskMPanel.getInfoExtractorMI().updateTaskStatus(Task.IS_RUNNING, infoExtractorCommonTask.getTask().getId());
          taskMPanel.updateTaskStatusByTaskName(taskName, Task.IS_RUNNING);
          //for taskExecutor add taskExecutorListener
          arg0.addTaskExecutorListener((TaskExecutorListener)new SwingTaskExecutorListener(jframe));
          InfoExtractTaskStatistics infoExtractTaskStatistics=new InfoExtractTaskStatistics();
          infoExtractTaskStatistics.setTaskName(infoExtractorCommonTask.getTask().getTaskName());
          taskMPanel.addEntryToTaskStatusListTable(infoExtractTaskStatistics);
          infoExtractorCommonTask.registerInfoExtractorCommonTaskListener(new SwingInfoExtractorCommonTaskListener(taskMPanel));
          
      }
     
      
	}
	@Override
	public void taskFailed(TaskExecutor arg0, Throwable arg1) {
		// TODO Auto-generated method stub
		 String taskName=null;
		 TaskMPanel taskMPanel=(TaskMPanel) jframe;
		  it.sauronsoftware.cron4j.Task task= arg0.getTask();
	      if(task.getClass().isAssignableFrom(InfoExtractorCommonTask.class)){
	    	  InfoExtractorCommonTask infoExtractorCommonTask=(InfoExtractorCommonTask) task;
	      
	       taskName=infoExtractorCommonTask.getTask().getTaskName();
          
	      taskMPanel.getInfoExtractorMI().updateTaskStatus(Task.IS_FAILED, infoExtractorCommonTask.getTask().getId());
	      taskMPanel.updateTaskStatusByTaskName(taskName, Task.IS_FAILED);

	      taskMPanel.removeTaskExecutorByTaskName(taskName);
	      mLog.info("任务：'"+taskName+"'运行失败，具体异常：'"+ExceptionUtil.getExceptionDetailsMessage(new Exception(arg1))+"'");
      
	      }
	      else{
	    	  //从子任务名称中获取真正的任务名称
	    	  taskName=taskMPanel.getTaskNameByTask(task);
	    	  if(taskName==null){
	    		  mLog.error("系统级别错误，任务名称不能为null！");
	    	  }
	    	  taskName=taskName.substring(0, taskName.lastIndexOf("#"));
		      mLog.info("任务：'"+taskName+"'运行失败，具体异常：'"+ExceptionUtil.getExceptionDetailsMessage(new Exception(arg1))+"'");

	      }
	   //   taskMPanel.getInfoExtractorMI().dropTable("news");

		
	}
	@Override
	public void taskSucceeded(TaskExecutor arg0) {
		// TODO Auto-generated method stub
		  it.sauronsoftware.cron4j.Task task= arg0.getTask();
	      if(task.getClass().isAssignableFrom(InfoExtractorCommonTask.class)){
	    	  InfoExtractorCommonTask infoExtractorCommonTask=(InfoExtractorCommonTask) task;
		  TaskMPanel taskMPanel=(TaskMPanel) jframe;
	      Task task2 = infoExtractorCommonTask.getTask();
		  String taskName=task2.getTaskName();

	      taskMPanel.getInfoExtractorMI().updateTaskStatus(Task.IS_RUNED, task2.getId());
	      taskMPanel.updateTaskStatusByTaskName(taskName, Task.IS_RUNED);
	      task2.setStatus(Task.IS_RUNED);
	      taskMPanel.removeTaskExecutorByTaskName(taskName);
          
	      mLog.info("任务：'"+taskName+"'运行成功！");
	      //运行成功的话就将采集结果转变成xml文件导出到指定目录
	      
	      try {
			taskMPanel.getInfoExtractorMI().exportDataToXmlFile(News.class, task2);
		} catch (TaskResultExportException e) {
			// TODO Auto-generated catch block
			mLog.info("任务："+taskName+"运行成功但是最总导出采集结果的时候出错！错误信息："+ExceptionUtil.getExceptionDetailsMessage(e)+"");
		}
	   //   taskMPanel.getInfoExtractorMI().dropTable("news");
	      }
		
	}

}